package com.rambo.weather;

import com.rambo.baselib.constant.ParamConstant;
import com.rambo.baselib.fraction.BaseFraction;
import com.rambo.baselib.mvp.BasePresenter;
import com.rambo.baselib.util.GpsUtil;
import com.rambo.baselib.util.Loading;
import com.rambo.baselib.util.LogUtil;
import com.rambo.weather.bean.WeatherBean;
import com.rambo.weather.mvp.IWeatherView;
import com.rambo.weather.mvp.WeatherPresenter;
import com.rambo.weather.provider.WeatherProvider;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.components.PageSliderIndicator;
import ohos.agp.components.PageSliderProvider;
import ohos.app.Context;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.bundle.IBundleManager;
import ohos.location.*;

import java.text.DecimalFormat;

/**
 * 天气  UI及素材 使用 https://github.com/wdsqjq/FengYunWeather
 * 开放API 使用 https://dev.qweather.com/docs/api/
 */
public class WeatherFraction extends BaseFraction<IWeatherView, WeatherPresenter> implements IWeatherView {
    ListContainer listContainer;
    PageSliderIndicator pageSliderIndicator;
    WeatherProvider weatherProvider;
    Locator locator;
    Location location;
    TaskDispatcher taskDispatcher ;

    public WeatherFraction(Context context) {
        super(context);
    }

    @Override
    protected void initView(Component component) {
        super.initView(component);
        listContainer = (ListContainer) component.findComponentById(ResourceTable.Id_list_container);
        pageSliderIndicator = (PageSliderIndicator) component.findComponentById(ResourceTable.Id_psi);
        weatherProvider = new WeatherProvider(this);
        listContainer.setItemProvider(weatherProvider);

        taskDispatcher = createParallelTaskDispatcher("WeatherFraction", TaskPriority.DEFAULT);
        verifyLocationPermission();
    }


    @Override
    protected int getLayoutResId() {
        return ResourceTable.Layout_fraction_weather;
    }

    @Override
    protected void fillData() {
        super.fillData();
    }

    @Override
    protected WeatherPresenter createPresenter() {
        return new WeatherPresenter();
    }

    @Override
    public void getWeatherResult(WeatherBean weatherBean) {
        hideLoading();
        if (weatherBean.nowResult == null || !weatherBean.nowResult.success()) {
            loading.updateLoadState(Loading.LoadingState.FAIL);
            return;
        }
        loading.updateLoadState(Loading.LoadingState.SUCCESS);
        weatherProvider.add(weatherBean);

    }



    /**
     * 校验定位权限
     */
    public void verifyLocationPermission() {
        if (weatherProvider == null) return;
        if (verifySelfPermission(ParamConstant.PERMISSION_LOCATION) != IBundleManager.PERMISSION_GRANTED) {
            if (canRequestPermission(ParamConstant.PERMISSION_LOCATION)) {
                requestPermissionsFromUser(new String[]{ParamConstant.PERMISSION_LOCATION}, ParamConstant.PERMISSION_LOCATION_CODE);
            }
        } else {
            if (weatherProvider.getCount() == 0) {
                //获取当前位置后，在获取天气数据
                if (locator == null) {
                    locator = new Locator(getAbilityInfo());
                    RequestParam param = new RequestParam(RequestParam.PRIORITY_FAST_FIRST_FIX, 0, 0);
                    try {
                        locator.startLocating(param, locatorCallback);
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    LocatorCallback locatorCallback = new LocatorCallback() {
        @Override
        public void onLocationReport(Location l) {
            if (l != null && location == null) {
                locator.stopLocating(this);
                GeoAddress geoAddress = GpsUtil.geoConvert(l);
                location = l;
                WeatherBean weatherBean = new WeatherBean();
                weatherBean.city = geoAddress != null ? geoAddress.getLocality() : "";
                DecimalFormat decimalFormat=new DecimalFormat("0.00");
                weatherBean.location =  decimalFormat.format(l.getLongitude())  + "," +  decimalFormat.format(l.getLatitude());
                weatherBean.isDialogStyle = false;
                presenter.getWeather(taskDispatcher, weatherBean);
            }
        }

        @Override
        public void onStatusChanged(int i) {

        }

        @Override
        public void onErrorReport(int i) {
            LogUtil.e("= onErrorReport==" + i);
        }
    };

    @Override
    protected void onStop() {
        super.onStop();
        if (locator != null) {
            locator.stopLocating(locatorCallback);
        }
    }
}
